package org.theSeed.pojo.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.theSeed.base.pojo.constant.SysConstant;
import org.theSeed.rbacBaseCommon.pojo.dto.CacheUserInfoDto;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统用户登录凭证
 */
public class UserInfoUserDetails implements UserDetails , CacheUserInfoDto<UserInfo> {
    public UserInfoUserDetails() {
    }

    public UserInfoUserDetails(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    private UserInfo userInfo;

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return userInfo.getRoleCodeList()
                .stream()
                .map(ele->{
                    return new SimpleGrantedAuthority(ele);
                }).collect(Collectors.toList());
    }

    @Override
    @JsonIgnore
    public String getPassword() {
        return userInfo.getPassword();
    }

    @Override
    public String getUsername() {
        return userInfo.getUsername();
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return SysConstant.ZERO.equals(userInfo.getLockFlag());
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return SysConstant.ONE.equals(userInfo.getEnableFlag());
    }

    @Override
    public String getUserKey() {
        return userInfo.getUsername();
    }

    @Override
    public UserInfo getUserInfo() {
        return userInfo;
    }

    @Override
    public String getSelectRoleKey() {
        return "demo";
    }

    @Override
    public List<String> getRoleKeyList() {
        return Arrays.asList();
    }
}
